package com.douyu.water.core.service;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
















import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.douyu.water.core.PagePara;
import com.douyu.water.core.dao.RTU_WMapper;
import com.douyu.water.core.data.DataMapper;
import com.douyu.water.core.data.RtuMapper;
import com.douyu.water.core.model.Data;
import com.douyu.water.core.model.DataExample;
import com.douyu.water.core.model.Rtu;
import com.douyu.water.core.model.WtldData;
import com.douyu.water.core.model.WtldDataExample;
import com.douyu.water.core.model.WtldRtu;
import com.douyu.water.core.model.WtldSzyData;
import com.douyu.water.core.model.WtldSzyDataExample;
import com.douyu.water.core.model.WtldSzyRtu;
import com.douyu.water.core.model.ZKData;
import com.douyu.water.core.model.ZKDataExample;
import com.douyu.water.core.model.ZKRtu;
import com.douyu.water.core.wtlddata.WtldMapper;
import com.douyu.water.core.wtldszydata.WtldSzyMapper;
import com.douyu.water.core.zkdata.ZKMapper;

@Service("dataService")
public class DataServiceImpl implements DataService {

	@Autowired
	private DataMapper dataMapper;
	@Autowired
	private ZKMapper zkMapper;
	@Autowired
	private WtldSzyMapper szyMapper;
	@Autowired
	private WtldMapper wtldMapper;
	@Autowired
	private RtuMapper rtuMapper;
	@Autowired
	private RTU_WMapper rtu_wMapper;
	@Resource
	private EquiService equiService;
	@Override
	
	
	/**
	 * 查询rtu设备表的数据
	 * **/
	public List<Data> select(Data record, PagePara page,int factory) {
		List<Data> list = null;
		DataExample example = createDataExample(record);
		if(page != null)
		{
			int total = dataMapper.countByExample(example);
			page.setTotal(total);
			example.setPagePara(page);
		}
		list = dataMapper.selectByExample(example);
		if(page != null)
		{
			list = processList(list,page);
		}
		if(list == null || list.size() == 0)//检索tf_common_data表数据
		{
			if(page != null)
			{
				int total = dataMapper.countByExampleCommon(example);
				page.setTotal(total);
				example.setPagePara(page);
			}
			list = dataMapper.selectByExampleCommon(example);
			if(page != null)
			{
				list = processList(list,page);
			}
		}
		for (Data data : list) {
			Rtu rtu=selectEqui(data,factory);
			if(rtu!=null){
				data.setStationname(rtu.getStationname());
			}
		}
		return list;
	}
	
	/**
	 * 查询中科水质数据表的数据
	 * 表tblCAIJISHUJU
	 * **/
	public List<ZKData> selectZKData(ZKData record,PagePara page){
		List<ZKData> list = null;
		ZKDataExample example = createZKDataExample(record);
		if(page != null)
		{
			int total = zkMapper.countByDataExample(example);
			page.setTotal(total);
			example.setPagePara(page);
		}
		list = zkMapper.selectByTYCode(example);
		ZKRtu rtu1=new ZKRtu();
		for (ZKData zk : list) {
			rtu1.setShebeicode(zk.getTongyicode());
			List<ZKRtu> rtu=equiService.selectZKRtu(rtu1, null, null);
			zk.setShebeimingcheng(rtu.get(0).getQuyu());
			
		}
		return list;
	}
	
	/**
	 * 查询大宇七参数水质数据表的数据
	 * 表
	 * **/
	public List<WtldSzyData> selectWtldSzyData(WtldSzyData record,PagePara page){
		List<WtldSzyData> list = null;
		WtldSzyDataExample example = createWtldSzyDataExample(record);
		example.setTableName("历史记录_0000"+record.getShebeiid()+"_2016");
		if(page != null)
		{
			int total = szyMapper.countByDataExample(example);
			page.setTotal(total);
			example.setPagePara(page);
		}
		list = szyMapper.selectData(example);
		WtldSzyRtu rtu1=new WtldSzyRtu();
		for (WtldSzyData szy : list) {
			rtu1.setId(szy.getShebeiid());
			List<WtldSzyRtu> rtu=equiService.selectWtldSzyRtu(rtu1, null, null);
			szy.setShebeimingcheng(rtu.get(0).getMingcheng());
			
		}
		return list;
	}
	
	/**
	 * 查询大宇三参数水质数据表的数据
	 * 表
	 * **/
	public List<WtldData> selectWtldData(WtldData record,PagePara page){
		List<WtldData> list = null;
		WtldDataExample example = createWtldDataExample(record);
		int id=Integer.parseInt(record.getDisijiid());
		if(id<100){
		example.setTableName("t_记录_定期存储_0000"+record.getDisijiid()+"_2016");
		}else{
		example.setTableName("t_记录_定期存储_000"+record.getDisijiid()+"_2016");	
		}
		if(page != null)
		{
			int total = wtldMapper.countByDataExample(example);
			page.setTotal(total);
			example.setPagePara(page);
		}
		list = wtldMapper.selectData(example);
		WtldRtu rtu1=new WtldRtu();
		for (WtldData wtld : list) {
			rtu1.setId(Integer.parseInt(wtld.getDisijiid()));
			System.out.println(rtu1.getId());
			List<WtldRtu> rtu=equiService.selectWtldRtu(rtu1, null, null);
			wtld.setShebeimingcheng(rtu.get(0).getHaopai());
			
		}
		return list;
	}
	/**
	 * 导出rtu设备表的数据data表
	 * **/
	public List<Data> exportRtu(Data record, PagePara page) {
		List<Data> list = null;
		DataExample example = createDataExample(record);
		if(page != null)
		{
			int total = dataMapper.countByExample(example);
			page.setTotal(total);
			example.setPagePara(page);
		}
		list = dataMapper.selectByExample(example);
		if(page != null)
		{
			list = processList(list,page);
		}
		for (Data data : list) {
			Rtu rtu=selectEqui(data,0);
			if(rtu!=null){
				data.setStationname(rtu.getStationname());
				
				data.setHighlevel(rtu.getHighlevel()/1000.00);
				data.setWaterdepth((rtu.getHighlevel()-data.getWaterlevel())/1000);				
				data.setSensordepth(rtu.getSensordepth()/1000.00);		
				data.setSim(rtu.getSimnumber());
				data.setSetuptime(rtu.getSetuptime());
				data.setGroundhigh(rtu.getGroundhigh()/1000);
			}
			
			data.setWaterlevel(data.getWaterlevel()/1000.00);
			data.setWatertemperature(data.getWatertemperature()/10.00);	
		}
		return list;
	}
	/**
	 * 导出tf-common-data表的数据
	 * tf_common_rtu设备表的数据
	 * 
	 * 
	 * **/
	public List<Data> exportCommon(Data record, PagePara page,int factory) {
		List<Data> list = null;
		DataExample example = createDataExample(record);
		if(page != null)
		{
			int total = dataMapper.countByExampleCommon(example);
			page.setTotal(total);
			example.setPagePara(page);
		}
		list = dataMapper.selectByExampleCommon(example);
		if(page != null)
		{
			list = processList(list,page);
		}for (Data data : list) {
			Rtu rtu=selectEqui(data,0);//##########################################################
			if(rtu!=null){
				data.setStationname(rtu.getStationname());
				if(rtu.getHighlevel()!=0){
				data.setHighlevel(rtu.getHighlevel()/1000.00);
				data.setWaterdepth((rtu.getHighlevel()-data.getWaterlevel())/1000);
				}else{
					data.setWaterdepth((0-data.getWaterlevel())/1000);
				}
				if(rtu.getSensordepth()!=0){
				data.setSensordepth(rtu.getSensordepth()/1000.00);
				}
				data.setSim(rtu.getSimnumber());
				data.setSetuptime(rtu.getSetuptime());
				data.setGroundhigh(rtu.getGroundhigh()/1000);
			}
			
			data.setWaterlevel(data.getWaterlevel()/1000.00);
			data.setWatertemperature(data.getWatertemperature()/10.00);	
		}
		return list;
	}
	
	//查询水位的前两条数据
	public List<Data> selectTop(Data record, PagePara page) {
		DataExample example = createDataExample(record);
		List<Data> data=dataMapper.selectTop(example);
		if(data==null||data.size()==0){
		data=dataMapper.selectTopByCommon(example);	
		}
		return data;
	}
	//查询中科水质的前两条数据
	public List<ZKData> selectZKTop(ZKData record, PagePara page) {
		ZKDataExample example = createZKDataExample(record);
		List<ZKData> data=zkMapper.selectTop(example);
		return data;
	}
	//查询大宇三参数水质的前两条数据
	public List<WtldData> selectWtldTop(WtldData record, int id) {
			WtldDataExample example = createWtldDataExample(record);
			if(id<100){
			example.setTableName("t_记录_定期存储_0000"+id+"_2016");
			}else{
			example.setTableName("t_记录_定期存储_000"+id+"_2016");	
			}
			List<WtldData> data=wtldMapper.selectTop(example);
			return data;
		}
	//查询大宇七参数水质的前两条数据
	public List<WtldSzyData> selectWtldSzyTop(WtldSzyData record) {
				WtldSzyDataExample example = createWtldSzyDataExample(record);
				example.setTableName("历史记录_0000"+record.getShebeiid()+"_2016");
				List<WtldSzyData> data=szyMapper.selectTop(example);
				return data;
			}
	@Override
	/**
	 * 查询tf-common-data表
	 * 
	 * 
	 * **/
	public List<Data> selectCommon(Data record, PagePara page) {
		List<Data> list = null;
		DataExample example = createDataExample(record);
		if(page != null)
		{
			int total = dataMapper.countByExampleCommon(example);
			page.setTotal(total);
			example.setPagePara(page);
		}
		list = dataMapper.selectByExampleCommon(example);
		if(page != null)
		{
			list = processList(list,page);
		}
		for (Data data : list) {
			Rtu rtu=selectEqui(data,0);
			if(rtu!=null){
				data.setStationname(rtu.getStationname());
			}
		}
		return list;
	}
	/**
	 * 截取分页数据
	 * @param list List<Data>  
	 *        -要截取的数据
	 * @param page PagePara
	 *        -分页数据
	 * @return
	 */
	private List<Data> processList(List<Data> list,PagePara page)
	{
		List<Data> data = new ArrayList<Data>();
		if(list == null || list.size() == 0)
		{
			return null;
		}
		if(page == null)
		{
			return list;
		}
		int start = page.getRecordStart();
		int end = page.getRecordEnd();
		int len = list.size();
		for(;start < end;start++)
		{
			if(start >= len)
			{
				break;
			}
			else
		    {
				data.add(list.get(start));	
		    }
		}
		return data;
	}
	@Override
	/**
	 * 根据data查询rtu
	 * **/
	public Rtu selectEqui(Data record,int factory)
	{
		Rtu rtu = null;
		Rtu records=new Rtu();
		records.setStationnumber(record.getStationnumber());
			records.setFactory(factory);
		if(record == null || StringUtils.isBlank(record.getStationnumber()))
		{
			return rtu;
		}
		rtu = rtu_wMapper.selectByPrimaryKey(record.getStationnumber());
		if(rtu == null)
		{
			List<Rtu> rtus=rtu_wMapper.selectByPrimaryKeyRtu(records);
			if(rtus!=null&&rtus.size()>0){
			rtu = rtus.get(0);
			}
		}
		return rtu;
	}
	@Override
	/**
	 * 查询返回map集合，用于曲线图
	 * **/
	public Map<String,String> selectLine(Data record,int factory)
	{
		List<Data> list = null;
		Date dateStart = record.getDatetime();
		Date dateEnd = record.getDatetimeend();
		if(dateStart == null && dateEnd == null)
		{
			dateStart = new Date();
			Calendar c = Calendar.getInstance();
			c.setTime(dateStart);
			c.add(Calendar.MONTH, -1);
			record.setDatetime(c.getTime());
		}
		DataExample example = createDataExample(record);
		list = dataMapper.selectByExample(example);
//		for (int i = 0; i < example.getOredCriteria().size(); i++) {
//			for (int j = 0; j < example.getOredCriteria().get(i).getCriteria().size(); j++) {
//				System.out.println(example.getOredCriteria().get(i).getCriteria().get(j).getCondition());
//				System.out.println(example.getOredCriteria().get(i).getCriteria().get(j).getValue());
//			}
//			
//		}
		if(list == null || list.size() == 0)
		{
			list =dataMapper.selectByExampleCommon(example);
			
		}
		
		for (Data data : list) {	
			data.setWaterlevel(data.getWaterlevel()/1000.00);
			data.setWatertemperature(data.getWatertemperature()/10.00);	
		}	
		for (int i = 0; i <list.size()-1; i++) {		
			
			Rtu rtu=selectEqui(list.get(i),factory);
				if(list.get(i).getDatetime().equals(list.get(i+1).getDatetime())){
					list.get(i).setWaterlevel((list.get(i).getWaterlevel()+list.get(i+1).getWaterlevel())/2);
					if(rtu!=null){
					list.get(i).setWaterlevel((rtu.getHighlevel()/1000)-list.get(i).getWaterlevel());
					}else{
						list.get(i).setWaterlevel(0-list.get(i).getWaterlevel());
					}
					list.get(i).setWatertemperature((list.get(i).getWatertemperature()+list.get(i+1).getWatertemperature())/2);
					list.remove(i+1);
					}
		}
		return processList(list);
	}
	
	/**
	 * 检索中科水质曲线图数据
	 * @param record
	 * @return
	 */
	public Map<String,String> selectZKLine(ZKData record){
		List<ZKData> list = null;
		Date dateStart = record.getCaijishijian();
		Date dateEnd = record.getCaijishijianend();
		if(dateStart == null && dateEnd == null)
		{
			dateStart = new Date();
			Calendar c = Calendar.getInstance();
			c.setTime(dateStart);
			c.add(Calendar.MONTH, -1);
			record.setCaijishijian(c.getTime());
		}
		ZKDataExample example = createZKDataExample(record);
		list = zkMapper.selectByTYCode(example);	
		return processZKList(list);
	}
	/**
	 * 检索大宇三参数曲线图数据
	 * @param record
	 * @return
	 */
	public Map<String,String> selectWtldLine(WtldData record){
		List<WtldData> list = null;
		Date dateStart = record.getCaijishijian();
		Date dateEnd = record.getCaijishijianend();
		if(dateStart == null && dateEnd == null)
		{
			dateStart = new Date();
			Calendar c = Calendar.getInstance();
			c.setTime(dateStart);
			c.add(Calendar.MONTH, -1);
			record.setCaijishijian(c.getTime());
		}
		WtldDataExample example = createWtldDataExample(record);
		example.setTableName("t_记录_定期存储_000"+record.getDisijiid()+"_2016");
		list = wtldMapper.selectData(example);	
		return processWtldList(list);
		
	}
	/**
	 * 检索大宇七参数曲线图数据
	 * @param record
	 * @return
	 */
	public Map<String,String> selectWtldSzyLine(WtldSzyData record){
		List<WtldSzyData> list = null;
		Date dateStart = record.getCaijishijian();
		Date dateEnd = record.getCaijishijianend();
		if(dateStart == null && dateEnd == null)
		{
			dateStart = new Date();
			Calendar c = Calendar.getInstance();
			c.setTime(dateStart);
			c.add(Calendar.MONTH, -1);
			record.setCaijishijian(c.getTime());
		}
		WtldSzyDataExample example = createWtldSzyDataExample(record);
		example.setTableName("历史记录_0000"+record.getShebeiid()+"_2016");
		list = szyMapper.selectData(example);	
		return processWtldSzyList(list);
	}
	
	/**
	 * 转换方法，将list集合转成map集合
	 * **/
	public Map<String,String> processList(List<Data> list)
	{
		Map<String,String> data = new HashMap<String,String>();
		if(list == null || list.size() == 0)
		{
			return data;
		}
		int size = list.size();
		StringBuffer lev = new StringBuffer();
		StringBuffer wtemp = new StringBuffer();
		for(int i = 0;i < size;i++)
		{
			Data temp = list.get(i);
			String date = getDateStr(temp.getDatetime());
			if(StringUtils.isBlank(date))
			{
				continue;
			}
			double level = temp.getWaterlevel();
			double tempera = temp.getWatertemperature();
			if(i == (size - 1))
			{
				lev.append("[" + date + "," + level + "]");
				wtemp.append("[" + date + "," + tempera + "]");
			}
			else
			{
				lev.append("[" + date + "," + level + "],");
				wtemp.append("[" + date + "," + tempera + "],");
			}
		}
		data.put("level", lev.toString());		
		data.put("temp", wtemp.toString());
		return data;
	}
	
	/**
	 * 转换方法，将list集合转成map集合
	 * **/
	public Map<String,String> processZKList(List<ZKData> list)
	{
		Map<String,String> data = new HashMap<String,String>();
		if(list == null || list.size() == 0)
		{
			return data;
		}
		int size = list.size();
		StringBuffer biaogao = new StringBuffer();
		StringBuffer maishen = new StringBuffer();
		StringBuffer diandaolv1 = new StringBuffer();
		StringBuffer wendu1 = new StringBuffer();
		for(int i = 0;i < size;i++)
		{
			ZKData temp = list.get(i);
			String date = getDateStr(temp.getCaijishijian());
			if(StringUtils.isBlank(date))
			{
				continue;
			}
			double shuiweibiaogao = temp.getShuiweimaishen();
			double shuiweimaishen = temp.getShuiweibiaogao();
			double diandaolv = temp.getDiandaolv();
			double wendu = temp.getWendu();
			if(i == (size - 1))
			{
				maishen.append("[" + date + "," + shuiweibiaogao + "]");
				biaogao.append("[" + date + "," + shuiweimaishen + "]");
				diandaolv1.append("[" + date + "," + wendu + "]");
				wendu1.append("[" + date + "," + diandaolv + "]");
			}
			else
			{
				maishen.append("[" + date + "," + shuiweibiaogao + "],");
				biaogao.append("[" + date + "," + shuiweimaishen + "],");
				diandaolv1.append("[" + date + "," + diandaolv + "],");
				wendu1.append("[" + date + "," + wendu + "],");
			}
		}
		data.put("shuiweimaishen", maishen.toString());		
		data.put("shuiweibiaogao", biaogao.toString());
		data.put("diandaolv", diandaolv1.toString());		
		data.put("wendu", wendu1.toString());
		return data;
	}
	
	/**
	 * 转换方法，将list集合转成map集合
	 * **/
	public Map<String,String> processWtldList(List<WtldData> list)
	{
		Map<String,String> data = new HashMap<String,String>();
		if(list == null || list.size() == 0)
		{
			return data;
		}
		int size = list.size();
		StringBuffer shuiweiji = new StringBuffer();
		StringBuffer diandaolv1 = new StringBuffer();
		StringBuffer shuiwen1 = new StringBuffer();
		for(int i = 0;i < size;i++)
		{
			WtldData temp = list.get(i);
			String date = getDateStr(temp.getCaijishijian());
			if(StringUtils.isBlank(date))
			{
				continue;
			}
			double shuiwei = temp.getShuiwei();
			double shuiwen = temp.getShuiwen();
			double diandaolv = temp.getDiandaolv();
			if(i == (size - 1))
			{
				shuiweiji.append("[" + date + "," + shuiwei + "]");
				diandaolv1.append("[" + date + "," + diandaolv + "]");
				shuiwen1.append("[" + date + "," + shuiwen + "]");
			}
			else
			{
				shuiweiji.append("[" + date + "," + shuiwei + "],");
				shuiwen1.append("[" + date + "," + shuiwen + "],");
				diandaolv1.append("[" + date + "," + diandaolv + "],");
			}
		}
		data.put("shuiweiji", shuiweiji.toString());		
		data.put("diandaolv", diandaolv1.toString());		
		data.put("shuiwen", shuiwen1.toString());
		return data;
	}
	
	/**
	 * 转换方法，将list集合转成map集合
	 * **/
	public Map<String,String> processWtldSzyList(List<WtldSzyData> list)
	{
		Map<String,String> data = new HashMap<String,String>();
		if(list == null || list.size() == 0)
		{
			return data;
		}
		int size = list.size();
		StringBuffer wendu1 = new StringBuffer();
		StringBuffer ph1 = new StringBuffer();
		StringBuffer diandaolv1 = new StringBuffer();
		StringBuffer yandu1 = new StringBuffer();
		StringBuffer kuanghuawu1 = new StringBuffer();
		StringBuffer lvhuawu1= new StringBuffer();
		StringBuffer shendu1 = new StringBuffer();
		for(int i = 0;i < size;i++)
		{
			WtldSzyData temp = list.get(i);
			String date = getDateStr(temp.getCaijishijian());
			if(StringUtils.isBlank(date))
			{
				continue;
			}
			double wendu = temp.getWendu();
			double ph = temp.getPh();
			double diandaolv = temp.getDiandaolv();
			double yandu = temp.getYandu();
			double kuanghuawu = temp.getKuanghuawu();
			double lvhuawu = temp.getLvhuawu();
			double shendu = temp.getShendu();
			if(i == (size - 1))
			{
				wendu1.append("[" + date + "," + wendu + "]");
				ph1.append("[" + date + "," + ph + "]");
				diandaolv1.append("[" + date + "," + diandaolv + "]");
				yandu1.append("[" + date + "," + yandu + "]");
				kuanghuawu1.append("[" + date + "," + kuanghuawu + "]");
				lvhuawu1.append("[" + date + "," + lvhuawu + "]");
				shendu1.append("[" + date + "," + shendu + "]");
			}
			else
			{
				wendu1.append("[" + date + "," + wendu + "],");
				ph1.append("[" + date + "," + ph + "],");
				diandaolv1.append("[" + date + "," + diandaolv + "],");
				yandu1.append("[" + date + "," + yandu + "],");
				kuanghuawu1.append("[" + date + "," + kuanghuawu + "],");
				lvhuawu1.append("[" + date + "," + lvhuawu + "],");
				shendu1.append("[" + date + "," + shendu + "],");
			}
		}
		data.put("wendu", wendu1.toString());		
		data.put("ph", ph1.toString());
		data.put("diandaolv", diandaolv1.toString());		
		data.put("yandu", yandu1.toString());
		data.put("kuanghuawu", kuanghuawu1.toString());		
		data.put("lvhuawu", lvhuawu1.toString());
		data.put("shendu", shendu1.toString());	
		return data;
	}
	
	private String getDateStr(Date date)
	{
		if(date == null)
		{
			return null;
		}
		String temp = null;
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		temp ="Date.UTC("+c.get(Calendar.YEAR)+"," + c.get(Calendar.MONTH) + ","+c.get(Calendar.DAY_OF_MONTH)+",";
		temp += c.get(Calendar.HOUR_OF_DAY)+","+c.get(Calendar.MINUTE) +"," + c.get(Calendar.SECOND) + ")";
		return temp;
	}
	/**
	 * 形成检索条件
	 * @param record Data
	 *        -检索条件
	 * @return
	 */
	private DataExample  createDataExample(Data record)
	{
		DataExample example = new DataExample();
		DataExample.Criteria criteria = example.createCriteria();
		if(record != null)
		{
			if(!StringUtils.isBlank(record.getStationnumber()))
			{
				criteria.andStationnumberEqualTo(record.getStationnumber().trim());
			}
			
			if(record.getDatetime() != null && record.getDatetimeend() != null)
			{
				criteria.andDatetimeBetween(record.getDatetime(), record.getDatetimeend());
				
			}
			else if(record.getDatetime() != null)
			{
				criteria.andDatetimeGreaterThanOrEqualTo(record.getDatetime());
			}
			else if(record.getDatetimeend() != null)
			{
				criteria.andDatetimeLessThanOrEqualTo(record.getDatetimeend());
			}
		}
		example.setOrderByClause(" Datetime desc");
		return example;
	}
	/**
	 * 形成中科水质检索条件
	 * @param record Data
	 *        -检索条件
	 * @return
	 */
	private ZKDataExample  createZKDataExample(ZKData record)
	{
		ZKDataExample example = new ZKDataExample();
		ZKDataExample.Criteria criteria = example.createCriteria();
		if(record != null)
		{
			if(!StringUtils.isBlank(record.getTongyicode()))
			{
				criteria.andTongyicodeLike("%"+record.getTongyicode().trim()+"%");
			}
			if(record.getCaijishijian() != null && record.getCaijishijianend() != null)
			{
				criteria.andCaijishijianBetween(record.getCaijishijian(), record.getCaijishijianend());
				
			}
			else if(record.getCaijishijian() != null)
			{
				criteria.andCaijishijianGreaterThanOrEqualTo(record.getCaijishijian());
			}
			else if(record.getCaijishijianend() != null)
			{
				criteria.andCaijishijianLessThanOrEqualTo(record.getCaijishijianend());
			}
		}else{
			 criteria = example.createCriteria();
			criteria.andTongyicodeLike("%37%");
		}
		example.setOrderByClause(" CAIJISHIJIAN desc");
		return example;
	}
	/**
	 * 形成大宇三参数水质检索条件
	 * @param record Data
	 *        -检索条件
	 * @return
	 */
	private WtldDataExample  createWtldDataExample(WtldData record)
	{
		WtldDataExample example = new WtldDataExample();
		WtldDataExample.Criteria criteria = example.createCriteria();
		if(record != null)
		{
			criteria.andChangyong();
//			if(record.getShebeiid()!=0)
//			{
//				criteria.andIDLike("%"+record.getShebeiid()+"%");
//			}
			
			if(record.getCaijishijian() != null && record.getCaijishijianend() != null)
			{
				criteria.andCaijishijianBetween(record.getCaijishijian(), record.getCaijishijianend());				
			}
			else if(record.getCaijishijian() != null)
			{
				criteria.andCaijishijianGreaterThanOrEqualTo(record.getCaijishijian());
			}
			else if(record.getCaijishijianend() != null)
			{
				criteria.andCaijishijianLessThanOrEqualTo(record.getCaijishijianend());
			}
		}else{
			 criteria = example.createCriteria();
			 criteria.andChangyong();
		}
		example.setOrderByClause(" 采集时间 desc");
		return example;
	}
	
	/**
	 * 形成大宇七参数水质检索条件
	 * @param record Data
	 *        -检索条件
	 * @return
	 */
	private WtldSzyDataExample  createWtldSzyDataExample(WtldSzyData record)
	{
		WtldSzyDataExample example = new WtldSzyDataExample();
		WtldSzyDataExample.Criteria criteria = null;
		if(record != null)
		{
			criteria=example.createCriteria();
			criteria.andChangyong();
			if(record.getShebeiid()!=0)
			{
				criteria.andIDLike("%"+record.getShebeiid()+"%");
			}
			
			if(record.getCaijishijian() != null && record.getCaijishijianend() != null)
			{
				criteria.andCaijishijianBetween(record.getCaijishijian(), record.getCaijishijianend());				
			}
			else if(record.getCaijishijian() != null)
			{
				criteria.andCaijishijianGreaterThanOrEqualTo(record.getCaijishijian());
			}
			else if(record.getCaijishijianend() != null)
			{
				criteria.andCaijishijianLessThanOrEqualTo(record.getCaijishijianend());
			}
		}else{
			 criteria = example.createCriteria();
			 criteria.andChangyong();
		}
		example.setOrderByClause(" 采集时间 desc");
		return example;
	}
	/**
	 * 将两个集合合并
	 * **/
	private void union(List<Data> list,List<Data> list2)
	{
		if((list == null || list.size() == 0 ) && (list2 == null || list2.size() == 0))
		{
			return;
		}
		if(list == null && list2 != null)
		{
			list = list2;
			return;
		}
		if(list2!=null){
		for(Data data : list2)
		{
			list.add(data);
		}
		}
	}

	@Override
	/**
	 * 导出数据查询
	 * **/
	public List<Data> selectExportData(Data record) {
		List<Data> list = null;
	     Calendar   calendar   =   new   GregorianCalendar(); 
	     if(record.getDatetimeend()!=null){
	     calendar.setTime(record.getDatetimeend()); 
	     }
	     calendar.add(calendar.DATE,1);//把日期往后增加一天.整数往后推,负数往前移动 
		record.setDatetimeend(calendar.getTime());
		if(record.getStationnumber()==""){
			record.setStationnumber(null);
		}
		if(record.getStationnumber()!=null){
		record.setStationnumber(record.getStationnumber().substring(0,4)+"%");
		}
		//processData(record);
		DataExample example =createDataExample(record);
		list = dataMapper.selectAllData(record);
		for (Data data : list) {
			Rtu rtu=selectEqui(data,0);
			if(rtu!=null){
				if(rtu.getHighlevel()!=0){
				data.setHighlevel(rtu.getHighlevel()/1000.00);
				data.setWaterdepth((rtu.getHighlevel()-data.getWaterlevel())/1000);
				}else{
					data.setWaterdepth((0-data.getWaterlevel())/1000);
				}
				if(rtu.getSensordepth()!=0){
				data.setSensordepth(rtu.getSensordepth()/1000.00);
				}
				if(rtu.getFactory()==null){
					data.setFactname("正元冶达");
				}
				else if(rtu.getFactory()==2){
					data.setFactname("中科光大");
				}else if(rtu.getFactory()==3){
					data.setFactname("博兴大宇");
				}
				data.setSim(rtu.getSimnumber());
				data.setSetuptime(rtu.getSetuptime());
				data.setGroundhigh(rtu.getGroundhigh()/1000);
			}
			
			data.setWaterlevel(data.getWaterlevel()/1000.00);
			data.setWatertemperature(data.getWatertemperature()/10.00);	
		}
		return list;
	}
	
	/**
	 * 处理检索条件，查看有没有选择时间
	 * @param record
	 */
	private void processData(Data record)
	{
		Date start = record.getDatetime();
		if(start == null)
		{
			Calendar c = Calendar.getInstance();
			c.set(c.get(Calendar.YEAR), 0, 1);
			record.setDatetime(c.getTime());
		}
		Date end = record.getDatetimeend();
		if(end == null)
		{
			record.setDatetimeend(new Date());
		}
		else
	    {
			Calendar dateend = Calendar.getInstance();
			dateend.setTime(end);
			dateend.add(Calendar.DAY_OF_MONTH,1);
			record.setDatetimeend(dateend.getTime());
	    }
	}
}
